<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 自定义组件，标题 -->
      <PageTitle :show-before="true">
        <template v-slot:before>
          <h2>{{ $t('route.jingang') }}</h2>
        </template>
        <template v-slot:after>
          <el-button class="filter-item" size="medium" style="margin-left: 10px;" type="primary" icon="el-icon-edit" :disabled="!checkPermission('jingang-add')" @click="addOrEidt('add',0)">{{ $t('button.add') }}</el-button>
        </template>
      </PageTitle>
      <!-- 搜索筛选 -->
      <el-row :gutter="20" style="margin-top:20px">
        <el-col :span="4">
          <el-input v-model="query.name" type="text" :placeholder="$t('table.ForeignLanguageName')" />
        </el-col>
        <el-col :span="4">
          <el-input v-model="query.sub_name" type="text" :placeholder="$t('table.ChName')" />
        </el-col>
        <el-col :span="4">
          <el-select v-model.number="query.id" :placeholder="$t('table.selectThe')+$t('table.state')">
            <el-option v-for="(item,index) in statusData" :key="index" :label="item.value" :value="item.id" />
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" icon="el-icon-search" @click="search">{{ $t('button.search') }}</el-button>
        </el-col>
      </el-row>
      <!-- 数据表 -->
      <el-table v-loading="downloadLoading" border :data="list" max-height="650px" style="margin-top: 20px;">
        <el-table-column label="ID" prop="id" sortable="custom" width="60" />
        <el-table-column :label="$t('table.ForeignLanguageName')" prop="name" />
        <el-table-column :label="$t('table.ChName')" prop="sub_name" />
        <el-table-column :label="$t('table.state')" prop="status" width="120">
          <template slot-scope="{row}">
            <el-tag :type="row.hidden|statusFilter">
              {{ row.hidden|formStatusData }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.creationTime')" prop="created_at">
          <template slot-scope="{row}">
            {{ row.created_at|timeStanp }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.actions')" align="center" width="340" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button icon="el-icon-edit" type="primary" size="mini" :disabled="!checkPermission('jingang-edit')" @click="addOrEidt('edit2',row)">{{ $t('button.edit') }}</el-button>
            <el-button icon="el-icon-document" size="mini" type="warning" :disabled="!checkPermission('jingang-set')" @click="addOrEidt('show',row)"> 内容管理 </el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" :disabled="!checkPermission('jingang-del')" @click="delDataCate(row.id)">{{ $t('button.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" align="middle" style="margin-top:20px">
        <el-pagination background layout="total,prev,pager,next" :total="count" :page-size="query.limit" :current-page="query.page" @current-change="changPage" />
      </el-row>
    </div>
    <!-- 添加金刚区栏目 -->
    <el-dialog :title="dialogTitle" :visible="showDialog2" @opened="openDialog" @close="btnCanle2">
      <el-form ref="ruleForm2" :model="formData2" :rules="rules2" label-width="150px">
        <el-form-item :label="$t('table.ForeignLanguageName')">
          <el-input v-model="formData2.name" :placeholder="$t('table.enterThe')+' '+$t('table.ForeignLanguageName')" style="width:50%" />
        </el-form-item>
        <el-form-item :label="$t('table.ChName')">
          <el-input v-model="formData2.sub_name" :placeholder="$t('table.enterThe')+' '+$t('table.ChName')" style="width:50%" />
        </el-form-item>
        <el-form-item :label="$t('table.state')">
          <el-select v-model.number="formData2.hidden" :placeholder="$t('table.selectThe')+' '+$t('table.state')">
            <el-option v-for="(item,index) in statusData" :key="index" :label="item.value" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnCanle2">{{ $t('button.cancel') }}</el-button>
        <el-button type="primary" @click="btnOk2">{{ $t('button.confirm') }}</el-button>
      </div>
    </el-dialog>
    <!-- 添加 -->
    <el-dialog :title="dialogTitle1" :visible="showDialog" @opened="openDialog" @close="btnCanle">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="150px">
        <el-form-item :label="$t('table.ForeignLanguageName')">
          <el-input v-model="formData.name" :placeholder="$t('table.enterThe')+' '+$t('table.ForeignLanguageName')" style="width:50%" />
        </el-form-item>
        <el-form-item :label="$t('table.ChName')">
          <el-input v-model="formData.sub_name" :placeholder="$t('table.enterThe')+' '+$t('table.ChName')" style="width:50%" />
        </el-form-item>
        <el-form-item :label="$t('table.images')">
          <ImageUpload ref="staffPhoto" />
        </el-form-item>
        <el-form-item :label="$t('table.Gotopage')" prop="type_id">
          <el-select v-model.number="formData.type_id" :placeholder="$t('table.selectThe')+' '+$t('table.Gotopage')" @change="change">
            <el-option v-for="(item,index) in listApp" :key="index" :label="item.name" :value="item.id" />
            <el-option :label="$t('table.Web')" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="formData.type_id!==0" :label="$t('table.Parameter')" prop="parameter">
          <el-input v-model="formData.parameter" style="width: 30%;" :placeholder="$t('table.enterThe')+' '+$t('table.Parameter')" />
        </el-form-item>
        <el-form-item v-if="formData.type_id===0" :label="$t('table.hrefurl')" prop="page">
          <el-input v-model="formData.page" style="width: 50%;" :placeholder="$t('table.enterThe')+' '+$t('table.hrefurl')" />
        </el-form-item>
        <el-form-item :label="$t('table.state')">
          <el-select v-model.number="formData.hidden" :placeholder="$t('table.selectThe')+' '+$t('table.state')">
            <el-option v-for="(item,index) in statusData" :key="index" :label="item.value" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnCanle">{{ $t('button.cancel') }}</el-button>
        <el-button type="primary" @click="btnOk">{{ $t('button.confirm') }}</el-button>
      </div>
    </el-dialog>
    <!-- 打开内容 -->
    <el-dialog title="金刚区内容" :visible="showDialog1" width="70%" @close="btnCanle1">
      <el-table v-loading="downloadLoading1" border :data="listJin" max-height="550px" style="margin-top: 20px;">
        <el-table-column label="ID" prop="id" sortable="custom" width="60" />
        <el-table-column :label="$t('table.ForeignLanguageName')" prop="name" />
        <el-table-column :label="$t('table.ChName')" prop="sub_name" />
        <el-table-column :label="$t('table.images')" prop="icon">
          <template slot-scope="{row}">
            <div class="table-img"><img :src="row.icon" style="width: 100%;"></div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.state')" prop="status" width="120">
          <template slot-scope="{row}">
            <el-tag :type="row.hidden|statusFilter">
              {{ row.hidden|formStatusData }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="sort" :label="$t('table.sort')" width="250">
          <template slot-scope="{row}">
            <template v-if="row.isedit">
              <el-input v-model="row.sort" class="edit-input" size="small" style="width: 50%;" />
              <el-button
                style="margin-left: 10px;"
                class="cancel-btn"
                size="small"
                icon="el-icon-refresh"
                type="warning"
                @click="confirmEdit(row)"
              >
                {{ $t('button.confirm') }}
              </el-button>
            </template>
            <span v-else>{{ row.sort }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.creationTime')" prop="created_at" width="180">
          <template slot-scope="{row}">
            {{ row.created_at|timeStanp }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.actions')" align="center" width="380" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button icon="el-icon-edit" type="primary" size="mini" :disabled="!checkPermission('jingangC-edit')" @click="addOrEidt('edit',row)">{{ $t('button.edit') }}</el-button>
            <el-button v-if="row.isedit" type="warning" size="mini" @click="cancelEdit(row)">
              {{ $t('button.cancel') }}
            </el-button>
            <el-button v-else type="warning" size="mini" :disabled="!checkPermission('jingangC-set')" @click="paixu(row)">
              {{ $t('table.sort') }}
            </el-button>
            <el-button icon="el-icon-delete" type="dang er" size="mini" :disabled="!checkPermission('jingangC-del')" @click="delData(row.id)">{{ $t('button.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" align="middle" style="margin-top:20px">
        <el-pagination background layout="total,prev,pager,next" :total="count1" :page-size="query1.limit" :current-page="query1.page" @current-change="changPage1" />
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnCanle1">{{ $t('button.cancel') }}</el-button>
        <el-button type="primary" :disabled="!checkPermission('jingangC-add')" @click="addOrEidt('imgAdd',0)">{{ $t('button.add') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getJinGangCateList, getJinGangList, delJinGangCate, addJinGangCate, addJinGang, delJinGang, sortJinGang, getAppPage } from '@/api/user'
export default ({
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        2: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      downloadLoading: false,
      downloadLoading1: false,
      statusData: [{
        id: 1,
        value: this.$t('table.normal')
      },
      {
        id: 2,
        value: this.$t('table.deactivate')
      }],
      query: {
        id: undefined,
        name: '',
        sub_name: '',
        aid: 1,
        page: 1,
        limit: 10
      },
      query1: {
        id: undefined,
        jid: undefined,
        name: '',
        sub_name: '',
        aid: 1,
        page: 1,
        limit: 10
      },
      count1: 0,
      count: 0,
      dialogTitle: '新增金刚区栏目',
      dialogTitle1: '新增金刚区内容',
      formData: {
        aid: 1,
        id: undefined,
        jid: undefined,
        name: '',
        sub_name: '',
        hidden: undefined,
        sort: 0,
        icon: '',
        type: undefined,
        type_id: undefined,
        page: '',
        url_type: 2,
        parameter: undefined
      },
      rules: {
        name: [{ required: true, message: this.$t('table.name') + this.$t('prompt.noEmpty'), trigger: 'blur' }],
        hidden: [{ required: true, message: this.$t('table.state') + this.$t('prompt.noEmpty'), trigger: 'blur' }]
      },
      formData2: {
        aid: 1,
        id: undefined,
        name: '',
        sub_name: '',
        hidden: undefined
      },
      rules2: {
        name: [{ required: true, message: this.$t('table.name') + this.$t('prompt.noEmpty'), trigger: 'blur' }],
        hidden: [{ required: true, message: this.$t('table.state') + this.$t('prompt.noEmpty'), trigger: 'blur' }]
      },
      showDialog: false,
      showDialog1: false,
      showDialog2: false,
      listApp: [],
      listJin: [],
      jinId: undefined
    }
  },
  created() {
    // 获取userid
    this.getBusinessCateList()
    this.getAppList()
  },
  methods: {
    // 获取选择的值
    change(e) {
      this.listApp.forEach(element => {
        if (e === 0) {
          this.formData.type = 3
          return
        }
        if (element.id === e) {
          this.formData.type = element.type
          this.formData.page = element.page
          this.formData.url_type = element.url_type
        }
      })
    },
    // 获取页面列表
    async getAppList() { // 请求数据
      const one = {
        aid: 1,
        id: undefined
      }
      const { list } = await getAppPage(one)
      this.listApp = list
    },
    // 获取商户分了列表
    async getBusinessCateList() {
      this.downloadLoading = true
      const { count, list } = await getJinGangCateList(this.query)
      this.count = count
      // 给数组对象使用map循环添加新属性
      const lists = list.map((item, index) => {
        return Object.assign(item, { isedit: false })
      })
      this.list = lists
      this.downloadLoading = false
    },
    // 获取金刚区列表
    async getJingangList() {
      this.downloadLoading1 = true
      const { count, list } = await getJinGangList(this.query1)
      this.count1 = count
      // 给数组对象使用map循环添加新属性
      const lists = list.map((item, index) => {
        return Object.assign(item, { isedit: false })
      })
      this.listJin = lists
      this.downloadLoading1 = false
    },
    // 点击展开新增修改
    addOrEidt(type, row) {
      if (type === 'add') {
        this.dialogTitle = '新增金刚区栏目'
        this.showDialog2 = true
        this.$nextTick(() => {
          this.$refs['ruleForm2'].clearValidate()
        })
      } else if (type === 'edit') {
        this.dialogTitle1 = '修改金刚区内容'
        this.formData.id = row.id
        this.formData.jia = row.jid
        this.formData.name = row.name
        this.formData.hidden = row.hidden
        this.formData.sub_name = row.sub_name
        this.formData.icon = row.icon
        this.formData.sort = row.sort
        this.formData.type = row.type
        this.formData.type_id = row.type_id
        this.formData.page = row.page
        this.formData.parameter = row.parameter
        this.showDialog = true
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      } else if (type === 'edit2') {
        this.dialogTitle = '修改金刚区栏目'
        this.formData2.id = row.id
        this.formData2.name = row.name
        this.formData2.hidden = row.hidden
        this.formData2.sub_name = row.sub_name
        this.showDialog2 = true
        this.$nextTick(() => {
          this.$refs['ruleForm2'].clearValidate()
        })
      } else if (type === 'show') {
        this.jinId = row.id
        this.query1.jid = row.id
        this.showDialog1 = true
        this.getJingangList()
      } else if (type === 'imgAdd') {
        this.dialogTitle1 = '新增金刚区栏目'
        this.formData.jid = this.jinId
        this.showDialog = true
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
    },
    // 点击确定时
    async btnOk2() {
      try {
        // 校验整个表单
        await this.$refs.ruleForm2.validate()
        // 调用新增接口
        await addJinGangCate(this.formData2)
        // 更新数据
        this.getBusinessCateList()
        this.btnCanle2()
        this.$message.success(this.$t('prompt.success'))
      } catch (error) {
        this.$message.error(error)
      }
    },
    // 点击确定时
    async btnOk() {
      try {
        // 校验整个表单
        await this.$refs.ruleForm.validate()
        // 读取上传组件的数据
        const fileList = this.$refs.staffPhoto.fileList
        // 循环判断是否已经完成全部上传
        if (fileList.some(item => !item.upload)) {
          //  如果此时去找 upload为false的图片 找到了说明 有图片还没有上传完成
          this.$message.warning(this.$t('prompt.noImage'))
          return
        }
        this.formData = { ...this.formData, icon: fileList && fileList.length ? fileList[0].url : '' }
        // 调用新增接口
        await addJinGang(this.formData)
        // 更新数据
        this.getJingangList()
        this.btnCanle()
        this.$message.success(this.$t('prompt.success'))
      } catch (error) {
        this.$message.error(error)
      }
    },
    // 删除金刚区栏目
    async delDataCate(e) {
      const data = {
        id: e
      }
      try {
        await this.$confirm(this.$t('prompt.deleteit'))
        await delJinGangCate(data)
        this.getBusinessCateList()
        this.$message.success(this.$t('prompt.deletesuccess'))
      } catch (error) {
        this.$message.error(error)
      }
    },
    // 删除金刚区内容
    async delData(e) {
      const data = {
        id: e
      }
      try {
        await this.$confirm(this.$t('prompt.deleteit'))
        await delJinGang(data)
        this.getJingangList()
        this.$message.success(this.$t('prompt.deletesuccess'))
      } catch (error) {
        this.$message.error(error)
      }
    },
    // 取消重置原来的数据
    btnCanle() {
      this.formData = {
        id: undefined,
        aid: 1,
        name: '',
        sub_name: '',
        hidden: undefined,
        sort: 0,
        icon: '',
        type: undefined,
        type_id: undefined,
        page: '',
        parameter: undefined
      }
      this.$refs.staffPhoto.fileList = []
      this.$refs.ruleForm.clearValidate()// 重置校验结果
      this.showDialog = false
    },
    btnCanle1() {
      this.jinId = undefined
      this.listJin = []
      this.query1.page = 1
      this.query1.jid = undefined
      this.showDialog1 = false
    },
    btnCanle2() {
      this.formData1 = {
        id: undefined,
        aid: 1,
        name: '',
        sub_name: '',
        hidden: undefined
      }
      this.$refs.ruleForm2.clearValidate()// 重置校验结果
      this.showDialog2 = false
    },
    // 点击排序
    paixu(row) {
      row.isedit = true
    },
    // 取消排序
    cancelEdit(row) {
      row.isedit = false
    },
    // 确认排序
    async confirmEdit(row) {
      const data = {
        id: row.id,
        sort: parseInt(row.sort)
      }
      row.isedit = false
      try {
        await sortJinGang(data)
        this.$message.success(this.$t('prompt.paixuSuccess'))
        this.getJingangList()
      } catch (error) {
        this.$message.error(error)
      }
    },
    // 搜索
    search() {
      this.query.page = 1
      this.getBusinessCateList()
    },
    // 分页
    changPage(newPage) {
      this.query.page = newPage
      this.getBusinessCateList()
    },
    // 分页
    changPage1(newPage) {
      this.query1.page = newPage
      this.getJingangList()
    },
    openDialog() {
      // 判断有icon的数据
      if (this.formData.icon) {
        // 这里我们赋值，同时需要给赋值的地址一个标记 upload: true
        this.$refs.staffPhoto.fileList = [{ url: this.formData.icon, upload: true }]
      }
    }
  }
})
</script>
<style lang="scss">
.table-img{
  width: 80px;
}
</style>
